package com.link510.nbiot.cmcc.domain.device;

import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.annotation.JSONField;
import com.google.common.base.Strings;
import com.google.common.collect.Maps;
import com.link510.nbiot.cmcc.config.Config;
import com.link510.nbiot.cmcc.domain.BaseDTO;
import lombok.*;

import java.util.Map;

/**
 * @author cqnews
 */
@Data
@AllArgsConstructor
@NoArgsConstructor
@Builder
public class RegistryDeviceDTO implements BaseDTO {

    private static final long serialVersionUID = 6617371527156837565L;

    /**
     * 设备名称
     */
    @Builder.Default
    private String title = "";

    /**
     * 设备描述
     */
    @Builder.Default
    private String desc = "iniot device";

    /**
     * 设备标签，字符串数组,否
     */
    @Builder.Default
    private String tags = "";

    /**
     * 必填
     */
    @Builder.Default
    private String imei = "";

    /**
     * 必填
     */
    @Builder.Default
    private String imsi = "";

    /**
     * 设备接入协议，这里指定为: LWM2M，必填参数
     */
    @Setter(AccessLevel.PRIVATE)
    @Builder.Default
    private String protocol = "LWM2M";

    // 设备地理位置，格式为：{"lon": 106, "lat": 29, "ele": 370}，可填参数
    private JSONObject location;

    /**
     * 设备私密性（默认true)
     */
    @JSONField(name = "private")
    @Builder.Default
    private boolean private2 = true;

    /**
     * Auth_Code，由数字或字母组成，不超过16位
     */
    @Builder.Default
    private String auth = "";


    /**
     * Auth_Code，由数字或字母组成，不超过16位
     */
    @Builder.Default
    private boolean obsv = true;


    /**
     * 其他信息（JSON格式，可自定义）
     */
    @Builder.Default
    private String other = "";
    /**
     * 测试设备与芯片类型对应关系（取值1-6），详情见说明
     */
    @Builder.Default
    private int chip = 0;

    /**
     * DTLS加密所使用的PSK，长度8-16个字节，字母或数字组成
     */
    @Builder.Default
    private String psk = "";

    public RegistryDeviceDTO(String title, String imei, String imsi) {
        this.title = title;
        this.imei = imei;
        this.imsi = imsi;
    }

    @Override
    public String toUrl() {
        StringBuilder url = new StringBuilder(Config.getApiUrl());
        url.append("/devices");
        System.out.println(url);
        return url.toString();
    }

    @Override
    public Map<String, Object> toMaps() {

        Map<String, Object> maps = Maps.newHashMap();

        maps.put("title", title);

        if (!Strings.isNullOrEmpty(desc)) {
            maps.put("desc", desc);
        }

        if (!Strings.isNullOrEmpty(tags)) {
            maps.put("tags", tags);
        }
        maps.put("protocol", protocol);

        if (location != null) {
            maps.put("location", this.location.toJSONString());
        }

        if (!private2) {
            maps.put("private", private2);
        }

        Map<String, String> authInfo = Maps.newHashMap();

        authInfo.put(imei, imsi);
        maps.put("auth_info", authInfo);

        if (!Strings.isNullOrEmpty(auth)) {
            maps.put("auth", auth);
        }

        if (!obsv) {
            maps.put("obsv", obsv);
        }

        if (!Strings.isNullOrEmpty(other)) {
            maps.put("other", other);
        }

        if (chip > 0) {
            maps.put("chip", chip);
        }

        if (!Strings.isNullOrEmpty(psk)) {
            maps.put("psk", psk);
        }

        return maps;
    }
}
